//
//  MYRequest.m
//  JFTQRLOGIN
//
//  Created by mac on 2017/11/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MYRequest.h"
#import "ConfigureHeader.h"
@interface MYRequest()<NSURLSessionDelegate>

@end
@implementation MYRequest
NSURLSession*session =nil;
+(NSURLSession *)session{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    session =[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:[self new] delegateQueue:[NSOperationQueue mainQueue]];
    });
    return session;
}
+ (void)postWithUrl:(NSString *)url pamaterdic:(NSDictionary *)dic resultBlock:(void (^)(id))block errorBlock:(void (^)(NSError *))errorBlock{
    NSString *wUrl = [MAIN_HOST stringByAppendingString:url];
       NSMutableURLRequest *mutableUrlRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:wUrl] cachePolicy:1 timeoutInterval:25];
    mutableUrlRequest.HTTPMethod =@"POST";
    NSString *jsonStr =[self convertToJsonData:dic];
    NSLog(@"%@",dic);
    NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
      mutableUrlRequest.HTTPBody= data;
    [[[self session] dataTaskWithRequest:mutableUrlRequest  completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            errorBlock(error);
            NSLog(@"%@",error.userInfo);
            return ;
        }
        NSHTTPURLResponse *ponse =(NSHTTPURLResponse*)response;
        if (ponse.statusCode!=200) {
            errorBlock(error);
            return;
        }
        NSError*jsonError;
        id returnData =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
        NSLog(@"%@",returnData);
        if ([returnData isKindOfClass:[NSDictionary class]]) {
            block (returnData);
        }else{
//             NSString *expressbase = [JFTRSA rsaDecryptCiphertextString:[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]];
//            if (expressbase) {
//                NSData *cryData = [expressbase dataUsingEncoding:NSUTF8StringEncoding];
//                id returnDic =[NSJSONSerialization JSONObjectWithData:cryData options:0 error:nil];
//                block(returnDic);
//            }
        
        }
        
    }]resume];
}

+(NSString *)convertToJsonData:(NSDictionary *)dict

{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}
@end
